package com.poker.controller;

import com.poker.entity.ManagerInfo;
import com.poker.service.ManagerInfoService;
import com.poker.util.Misc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by lxs.
 * 2019/4/8 3:16 PM
 */
@Controller
@RequestMapping("/pwd")
public class PwdController extends BaseController{

    @Resource
    private ManagerInfoService managerInfoService;

    @RequestMapping("/edit")
    public String edit() {
        return managerPage("password/edit");
    }

    @ResponseBody
    @RequestMapping("/submit")
    public String submit(String oldPassword, String newPassword) {
        ManagerInfo loginManager = getLoginManager();

        if (!Misc.getShiroMd5Pwd(oldPassword, loginManager.getSalt(), 2).equals(loginManager.getPassWord())) {
            return toJson(false, "原密码输入错误", null);
        }


        String salt = Misc.randomCode(16);
        loginManager.setSalt(salt);
        loginManager.setPassWord(Misc.getShiroMd5Pwd(newPassword, salt, 2));
        managerInfoService.save(loginManager);

        return toJson(true, "成功", null);
    }

}
